package gee 

import (
	"net/http"
	"fmt"
)


type HandleFunc func(http.ResponseWriter, *http.Request)

type Engine struct {
	router map[string]HandleFunc

}

func New() *Engine {
	return &Engine{router:make(map[string]HandleFunc)}
}

func (e *Engine) addRoute(method string,pattern string,handle HandleFunc){
	e.router[method+"_"+pattern] = handle
}

func (e *Engine) GET(pattern string,handle HandleFunc){
	e.addRoute("GET",pattern,handle)
}

func (e *Engine) POST(pattern string,handle HandleFunc){
	e.addRoute("POST",pattern,handle)
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	key := r.Method+"_"+r.URL.Path
	fmt.Println(key)
	if handler,ok := e.router[key];ok{
		handler(w,r)
	}else{
		w.Write([]byte("404 NOT FOUND"))
	}
}
func (e *Engine) Run(addr string)(err error){
	return http.ListenAndServe(addr,e)
}